import { query,add,remove,edit ,queryResources,queryLogs,queryOneUsergroup } from '@/services/devices';
import moment from 'moment';
import {todayLastSecond} from '@/utils/utils.js'
import createRowData from '@/utils/createRowData';
export default {
  namespace: 'devices',
  state: {
    pageLoaded:false,
    tableY:300,
    data:[],
    scrollData:{x:0,y:0},
    selectedRowKeys: [],
    searchParams:{
      date:moment().subtract(1, "day").format("YYYY-MM-DD"),
      meter_number:'',
      user_number:'',
      address:'',
      vendor_id:'',
      status:'0',
      page: 1,
      per_page: 50,
    },
    meta: {total: 0, per_page: 50,current_page:1},
    status_count:{total_count:0,normal_upload_count:0},
    total_consumption:{cold_meter:{}},
    additional: {total_count: 0, online_count: 0},
    loading: true,
    editRecord:{},
    pickRowData:{
      number:'',
      address:'',
      data:[]
    },
    detail:{
      pageLoaded:false,
      isDetail:false,
      detailId:'',
      data1:[],
      activeKey:'1',
      data2:[],
    }

  },
  effects: {
    *changeIsDetail({ payload, callback }, { call, put ,select}) {
      console.log('changeIsDetail')
      const {detail} = yield select(state => state.devices)
      yield put({
        type: 'changeDetail',
        payload:{
          ...detail,
          isDetail:!detail.isDetail,
          detailId:payload.id,
        }
      });
    },
    *reset({ payload, callback }, { call, put }) {
      console.log('reset=============')
      yield put({
        type: 'resetData',
        payload:payload
      });
      if(callback) callback()
    },
    *changeTableY({ payload, callback }, { call, put }) {
      const tableY=document.querySelector('.bottom-analysis').getBoundingClientRect().top -
        document.querySelector('.react-grid-Container').getBoundingClientRect().top - 58;
        yield put({
          type: 'saveTableY',
          payload: tableY
        });
    },
    *changeSearchParams({ payload, callback }, { call, put }) {
     console.log('changeSearchParams')
      yield put({
        type: 'saveSearchParams',
        payload: payload
      });
    },
    *changeLoaded({ payload, callback }, { call, put }) {
      yield put({
        type: 'changePageLoaded',
        payload: true,
      });
    },
    *changeScroll({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeScrollData',
        payload: payload,
      });
    },
    *selectRow({payload, callback}, {call, put,select}) {
      const {selectedRowKeys} = yield select(state => state.devices)
      if (selectedRowKeys.indexOf(payload.id) >= 0) {
        selectedRowKeys.splice(selectedRowKeys.indexOf(payload.id), 1);
      } else {
        selectedRowKeys.push(payload.id);
      }
      yield put({
        type: 'saveSelectRow',
        payload: selectedRowKeys,
      });
    },
    *fetch({ payload,callback }, { call, put ,select}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const state = yield select(state => state.devices)//获取state中的数据
      payload=payload||state.searchParams;
      console.log('payload',payload)
      const response = yield call(query, payload);
      console.log(response)
      if(response.status===200){
        yield put({
          type: 'save',
          payload:  response.data
        });
        yield put({
          type: 'saveSearchParams',
          payload: payload
        });
        yield put({
          type: 'changeLoading',
          payload: false,
        });
        if (callback) callback();
      }
    },
    *fetchRowData({ payload,callback }, { call, put ,select}) {

    },
    *changeSelectRow({payload, callback}, {call, put,select}) {
      yield put({
        type: 'saveSelectRow',
        payload: payload.selectedIndex,
      });
      console.log('payload',payload)
      const response = yield call(queryResources, payload.row);
      console.log(response)
      if(response.status===200){
        yield put({
          type: 'saveRowData',
          payload: {
            number:payload.row.meter_number,
            address:payload.row.address,
            data:response.data
          }
        });
        if (callback) callback();
      }
    },

  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data,
        meta:action.payload.meta,
        status_count:action.payload.status_count,
        total_consumption:action.payload.total_consumption,
      };
    },
    saveRowData(state, action) {
      return {
        ...state,
        pickRowData:action.payload,
      };
    },
    saveTableY(state, action) {
      return {
        ...state,
        tableY: action.payload,
      };
    },
    saveOneRecord(state, action) {
      return {
        ...state,
        editRecord:action.payload
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changePageLoaded(state, action) {
      return {
        ...state,
        pageLoaded: action.payload,
      };
    },
    changeScrollData(state, action) {
      return {
        ...state,
        scrollData: action.payload,
      };
    },
    saveSearchParams(state, action) {
      return {
        ...state,
        searchParams: {
          ...state.searchParams,
          ...action.payload
        },
      };
    },
    resetData(state, action) {
      return  {
        pageLoaded:action.payload,
        tableY:state.tableY,
        data:[],
        scrollData:{x:0,y:0},
        selectedRowKeys: [],
        searchParams:{
          date:moment().subtract(1, "day").format("YYYY-MM-DD"),
          meter_number:'',
          user_number:'',
          address:'',
          status:'0',
          vendor_id:'',
          page: 1,
          per_page: 50,
        },
        meta: {total: 0, per_page: 50,current_page:1},
        status_count:{total_count:0,normal_upload_count:0},
        total_consumption:{cold_meter:{}},
        loading: true,
        editRecord:{},
        pickRowData:{
          number:'',
          address:'',
          data:[]
        },
        additional: {total_count: 0, online_count: 0},
        detail:{
          pageLoaded:false,
          isDetail:false,
          detailId:'',
          data1:[],
          activeKey:'1',
          data2:[],
        }
      };
    },
    saveResources(state, action) {
      return {
        ...state,
        detail:{
          ...state.detail,
          data1: action.payload.data,
        }
      };
    },
    saveLogs(state, action) {
      return {
        ...state,
        detail:{
          ...state.detail,
          data2: action.payload.data,
          meta2:action.payload.meta
        }
      };
    },
    saveSelectRow(state, action) {
      return {
        ...state,
        selectedRowKeys: action.payload,
      };
    },
    changeDetail(state, action) {
      return {
        ...state,
        detail:action.payload
      };
    },
    changeDetailTabActive(state, action) {
      return {
        ...state,
        detail:{
          ...state.detail,
          activeKey:action.payload
        }
      };
    },
    saveDetailSearchParams(state, action) {
      return {
        ...state,
        detail:{
          ...state.detail,
          searchParams2: action.payload,
        }

      };
    },
  },

};
